#include "./myWinManager.h"
#include "../../mydxlib.h"
#include "./myApplication.h"

namespace Framework
{
	MsgStatus Framework::myWinManager::handleDrawEvent( myEventManager* manager,Message* msg )
	{
		myWindowlet* p;
		Painter* painter;

		painter = Painter::castFrom(msg->getMessage());
		xAssert(painter);
		if(painter->begin())
		{
			myWindowlet* p = (myWindowlet*)m_Manager.getFirstChildOf(TreeManager::cast(this));
			while(p)
			{
				if(p->isVisible())
					p->handleSendEvent(manager,msg);
				p =  (myWindowlet*)m_Manager.getNextSiblingOf(TreeManager::cast(p));

			}
			painter->end();
		}
		return vContinue;
	}


	myWindowlet* myWinManager::getFirstWindow()
	{
		return (myWindowlet*)m_Manager.getFirstChildOf(this);
	}

	myWindowlet* myWinManager::getLastWindow()
	{
		return (myWindowlet*)m_Manager.getLastChildOf(this);
	}

	myWindowlet* myWinManager::getNextWindow( myWindowlet* p )
	{
		if(TreeManager::cast(p)->getParent() != this)
			return vNull;
		return (myWindowlet*)m_Manager.getNextSiblingOf(TreeManager::cast(p));
	}

	myWindowlet* myWinManager::getPrevWindow( myWindowlet* p )
	{
		if(TreeManager::cast(p)->getParent() != this)
			return vNull;
		return (myWindowlet*)m_Manager.getPrevSiblingOf(TreeManager::cast(p));
	}

	TVoid myWinManager::notifyInvalidateRect(TRect* rect )
	{
		Painter* painter = getApplication()->getPainter();
		MessagePtr message = skynew (myWindowletMessage(&myControl::handlePaint,this,painter));
		painter->setInvalidArea(rect);

		m_App->getEventManager()->handlePostEvent(message);
		return;
	}

	TVoid myWinManager::notifyInvalidateControl( myControl* ctrl )
	{
		Painter* painter = getApplication()->getPainter();
		MessagePtr message = skynew (myWindowletMessage(&myControl::handlePaint,this,painter));
		painter->setInvalidObject(ctrl);

		m_App->getEventManager()->handlePostEvent(message);
		return;
	}

	myWinManager::myWinManager():TreeNode()
	{
		m_Manager.setRoot(this);
	}

	TBool myWinManager::attachLast( myWindowlet* win )
	{
		return m_Manager.attachLastChildOf(TreeManager::cast(this),TreeManager::cast(win));
	}

	TBool myWinManager::attachFirst( myWindowlet* win )
	{
		return m_Manager.attachFirstChildOf(TreeManager::cast(this),TreeManager::cast(win));
	}

	TBool myWinManager::attachNextOf( myWindowlet* win,myWindowlet* next )
	{
		if(TreeManager::cast(win)->getParent() != TreeManager::cast(this))
			return vFalse;

		return m_Manager.attachNextSiblingOf(TreeManager::cast(win),TreeManager::cast(next));
	}
	TBool myWinManager::attachPrevOf( myWindowlet* win,myWindowlet* prev )
	{
		if(TreeManager::cast(win)->getParent() != this)
			return vFalse;

		return m_Manager.attachPrevSiblingOf(TreeManager::cast(win),TreeManager::cast(prev));
	}

	myWindowlet* myWinManager::detachAll()
	{
		return (myWindowlet*)m_Manager.detachAllChildrenOf(this);
	}

	myWindowlet* myWinManager::detachFirst( )
	{
		return (myWindowlet*)m_Manager.detachFirstChildOf(this);
	}

	myWindowlet* myWinManager::detachLast( )
	{
		return (myWindowlet*)m_Manager.detachLastChildOf(this);
	}

	myWindowlet* myWinManager::detach( myWindowlet* win )
	{
		if(TreeManager::cast(win)->getParent() != TreeManager::cast(this))
			return vNull;
		return (myWindowlet*)m_Manager.detach(TreeManager::cast(win));
	}

	MsgStatus myWinManager::handleSendEvent( myEventManager* manager,Message* event )
	{
		if(event->getEvent() == (Handler::EventId)&myWindowlet::handlePaint)
			return handleDrawEvent(manager,event);
		/*switch(event->m_Type)
		{
		case EventType_UIDraw:
		return handleDrawEvent(manager,event);
		break;
		case EventType_KeyDown:
		case EventType_KeyUp:
		case EventType_KeyRepeat:
		case EventType_PenDown:
		case EventType_PenUp:
		return handleInputEvent(manager,event);
		case EventType_UIFocusNext:
		case EventType_UIFocusPrev:
		case EventType_UIChangeSize:
		return handleUIEvent(manager,event);
		break;
		}*/

		return vContinue;
	}
}